﻿using System;
using System.Web;
using System.Xml;

using umlungu.engineX.blogX;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington
	Started:	2008-06-14
	Status:		release	
	Version:	2.0.0
	Build:		20080614
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/

/*	-----------------------------------------------------------------------	
	Development Notes:
	==================
	20080614:	Initial release.
	---------------------------------------------------------------------------	*/

namespace umlungu.engineX.blogX.WebsiteX {
    /// <summary>
    /// This is a utility class to encapsulte profile and other functionality related to a user.
    /// Typically this class is created within brokers and the display classes
    /// </summary>
    public class WebsiteXUser{
        #region Invisible properties
        x_userprofile profile;
        #endregion

        #region Private constants
        private const string PATTERN_PUBLIC = "public";
        private const string PATTERN_CUSTOMER = "customer";
        #endregion

        #region Public constants
        public const string REMEMBER_ON = "on";
        public const string REMEMBER_OFF = "off";
        public const string PROPERTY_USERNAME = "Username";
        public const string PROPERTY_PASSWORD = "Password";
        public const string PROPERTY_TOKEN = "PXToken";
        public const string PROPERTY_FIRSTNAME = "FirstName";
        public const string PROPERTY_SURNAME = "Surname";
        public const string PROPERTY_FULLNAME = "Fullname";
        public const string PROPERTY_EMAIL = "Email";
        public const string PROPERTY_CELLNO = "Cellphone";
        public const string PROPERTY_USERID = "UserID";
        public const string PROPERTY_REMEMBER = "RememberMe";
        public const string PROPERTY_HINT_ID = "HintID";
        public const string PROPERTY_HINT_ANSWER = "HintAnswer";
        public const string PROPERTY_COOKIE = "TokenX";
        public const string PROPERTY_FILES_ALL = "FilesAll";
        public const string PROPERTY_FILES_VIDEO = "FilesVideo";
        public const string PROPERTY_FILES_VOICE = "FilesVoice";
        public const string PROPERTY_FRIENDID = "FriendID";
        public const string PROPERTY_FILEID = "FileID";
        public const string PROPERTY_FILEOWNERID = "FileOwnerID";
        #endregion

        #region Visible properties
        public string Username {
            get { return (profile.Get(PROPERTY_USERNAME) == null) ? "" : profile.Get(PROPERTY_USERNAME).InnerText; }
            set { profile.Add(PROPERTY_USERNAME, value); }
        }
        public string Password {
            get { return (profile.Get(PROPERTY_PASSWORD) == null) ? "" : profile.Get(PROPERTY_PASSWORD).InnerText; }
            set { profile.Add(PROPERTY_PASSWORD, value); }
        }
        public string Firstname {
            get { return (profile.Get(PROPERTY_FIRSTNAME) == null) ? "" : profile.Get(PROPERTY_FIRSTNAME).InnerText; }
            set { profile.Add(PROPERTY_FIRSTNAME, value); }
        }
        public string Surname {
            get { return (profile.Get(PROPERTY_SURNAME) == null) ? "" : profile.Get(PROPERTY_SURNAME).InnerText; }
            set { profile.Add(PROPERTY_SURNAME, value); }
        }
        public string Fullname {
            get { return (profile.Get(PROPERTY_FULLNAME) == null) ? "" : profile.Get(PROPERTY_FULLNAME).InnerText; }
            set { profile.Add(PROPERTY_FULLNAME, value); }
        }
        public string Email {
            get { return (profile.Get(PROPERTY_EMAIL) == null) ? "" : profile.Get(PROPERTY_EMAIL).InnerText; }
            set { profile.Add(PROPERTY_EMAIL, value); }
        }
        public string Cellno {
            get { return (profile.Get(PROPERTY_CELLNO) == null) ? "" : profile.Get(PROPERTY_CELLNO).InnerText; }
        }
        public string Token {
            get { return (profile.Get(PROPERTY_TOKEN) == null) ? "" : profile.Get(PROPERTY_TOKEN).InnerText; }
            set { profile.Add(PROPERTY_TOKEN, value); }
        }
        public string UserID {
            get { return (profile.Get(PROPERTY_USERID) == null) ? "" : profile.Get(PROPERTY_USERID).InnerText; }
            set { profile.Add(PROPERTY_USERID, value); }
        }
        public string RememberMe {
            get { return (profile.Get(PROPERTY_REMEMBER) == null) ? "" : profile.Get(PROPERTY_REMEMBER).InnerText; }
            set { profile.Add(PROPERTY_REMEMBER, value); }
        }
        public bool Remember {
            get { return (RememberMe == REMEMBER_ON); }
        }
        public string HintID {
            get { return (profile.Get(PROPERTY_HINT_ID) == null) ? "" : profile.Get(PROPERTY_HINT_ID).InnerText; }
            set { profile.Add(PROPERTY_HINT_ID, value); }
        }
        public string HintAnswer {
            get { return (profile.Get(PROPERTY_HINT_ANSWER) == null) ? "" : profile.Get(PROPERTY_HINT_ANSWER).InnerText; }
            set { profile.Add(PROPERTY_HINT_ANSWER, value); }
        }
        public string Cookie {
            get { return (profile.Get(PROPERTY_COOKIE) == null) ? "" : profile.Get(PROPERTY_COOKIE).InnerText; }
        }
        // Properties for files, video and voice mail
        public string AllFiles {
            get { return (profile.Get(PROPERTY_FILES_ALL) == null) ? "" : profile.Get(PROPERTY_FILES_ALL).InnerText; }
            set { profile.Add(PROPERTY_FILES_ALL, value); }
        }
        public string VideoFiles {
            get { return (profile.Get(PROPERTY_FILES_VIDEO) == null) ? "" : profile.Get(PROPERTY_FILES_VIDEO).InnerText; }
            set { profile.Add(PROPERTY_FILES_VIDEO, value); }
        }
        public string VoiceFiles {
            get { return (profile.Get(PROPERTY_FILES_VOICE) == null) ? "" : profile.Get(PROPERTY_FILES_VOICE).InnerText; }
            set { profile.Add(PROPERTY_FILES_VOICE, value); }
        }
        public bool IsCustomer {
            get { return (profile.LoginPattern == PATTERN_CUSTOMER); ; }
            set { profile.LoginPattern = (value) ? PATTERN_CUSTOMER : PATTERN_PUBLIC; }
        }
        // Property for friends and files
        public string FriendID {
            get { return (profile.Get(PROPERTY_FRIENDID) == null) ? "" : profile.Get(PROPERTY_FRIENDID).InnerText; }
            set { profile.Add(PROPERTY_FRIENDID, value); }
        }
        public string FileID {
            get { return (profile.Get(PROPERTY_FILEID) == null) ? "" : profile.Get(PROPERTY_FILEID).InnerText; }
            set { profile.Add(PROPERTY_FILEID, value); }
        }
        public string FileOwnerID {
            get { return (profile.Get(PROPERTY_FILEOWNERID) == null) ? "" : profile.Get(PROPERTY_FILEOWNERID).InnerText; }
            set { profile.Add(PROPERTY_FILEOWNERID, value); }
        }
        #endregion

        #region Constructors/Destructors
        /// <summary>Default constructor</summary>
        public WebsiteXUser()
        {
        }
        /// <summary>Constructor for derived classes</summary>
        public WebsiteXUser(x_userprofile uprofile){
            profile = uprofile;
        }
        #endregion

        #region Public methods
        /// <summary>
        /// Gets the value of a  named user profile parameter
        /// </summary>
        /// <param name="name">The name of the user profile parameter</param>
        /// <returns>The value of the user profile parameter</returns>
        public string Get(string name) {
            return (profile.Get(name) == null) ? "" : profile.Get(name).InnerText;
        }

        public void Set(string token, string userid) {
            Token = token;
            UserID = userid;
            profile.LoginPattern = PATTERN_CUSTOMER;
        }
        public void Set(string token, string userid, string uname, string pword) {
            Token = token;
            UserID = userid;
            Username = uname;
            Password = pword;
            profile.LoginPattern = PATTERN_CUSTOMER;
        }
        public void Set(string token, string userid, string uname, string pword, string fname, string sname) {
            Set(token, userid, uname, pword);
            Firstname = fname;
            Surname = sname;
            Fullname = String.Concat(fname, (sname != "") ? " " : "", sname);
        }
        public void SetName(string fname, string sname) {
            Firstname = fname;
            Surname = sname;
            Fullname = String.Concat(fname, (sname != "") ? " " : "", sname);
        }
        /// <summary>Clear essential profile values (ie not all eg retain 'user.Username' in profile if exists</summary>
        public void Reset() {
            profile.Clear(PROPERTY_TOKEN);
            profile.Clear(PROPERTY_PASSWORD);
            profile.LoginPattern = PATTERN_PUBLIC;
            RememberMe = REMEMBER_OFF;
        }
        #endregion

        #region Protected methods
        #endregion

        #region Private methods
        #endregion
    }
}
